using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using TrainingManagement.api.Models.Data.Readers;

namespace TrainingManagement.api.Models.Data.CRUD.TrainingRecord
{
    public class TrainingRecordSelectByRecordIdDataSource : QuerySource<int,List<BusinessObjects.TrainingRecord>>
    {
        public override List<BusinessObjects.TrainingRecord> Execute(int request)
        {
            const string selectByRecordId = "dbo.training_api_TrainingRecordSelectByRecordID";
            var parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@TrainingRecordID", SqlDbType.Int) {Value = request};

            using (var rdr = SqlHelper.ExecuteReader(
                ConnectionStrings.TrainingDb,
                CommandType.StoredProcedure, 
                selectByRecordId,
                parameters))
            {
                var reader = new TrainingRecordDataReader();
                var results = new List<BusinessObjects.TrainingRecord>();
                while (rdr.Read())
                {
                    results.Add(reader.Run(rdr));
                }
                return results;
            }
        }

    }
}